/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shattered2;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
 *
 * @author 10135228
 */
public class DrawPanel2 extends javax.swing.JPanel {

    private Color c1 = Color.LIGHT_GRAY;
    private Color c2 = Color.WHITE;
    private Color cm = Color.BLUE;
    
    private MainFrame parent = null;
    private int defaultX = 300*14;
    private int defaultY = 300*14;
    private int zoom = 1;
    private int clickPositionX = -1, clickPositionY = -1;

    public void setZoom(int zoom) {
        this.zoom = zoom;
        this.setPreferredSize(new Dimension(this.zoom*defaultX, this.zoom*defaultY));
        repaint();
    }
    
    /**
     * Creates new form DrawPanel2
     */

    
    public DrawPanel2(MainFrame parent, int zoom){
        this();
        this.parent = parent;
        this.zoom = zoom;
        this.setPreferredSize(new Dimension(zoom*defaultX, zoom*defaultY));
    }
    public DrawPanel2() {
        initComponents();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.scale(zoom, zoom);
        g2.setColor(c1);
        for (int i = 0; i < 300*14; i+=14) {
            if(g2.getColor().equals(c1)){
                g2.setColor(c2);  
            }else{
                g2.setColor(c1);
            }
            for (int j = 0; j < 300*14; j+=14) {
                g2.fillRect(j, i, 14, 14);                               
                if(g2.getColor().equals(c1)){
                    g2.setColor(c2);  
                }else{
                    g2.setColor(c1);
                }
                                
            }
            
        }
        if(clickPositionX >= 0 && clickPositionY >= 0){
            g2.setColor(cm);
            g2.fillRect(clickPositionX*14, clickPositionY*14, 14, 14);
        }
        drawPuzzle(g2,parent.getFieldData());
    }

    
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        FormListener formListener = new FormListener();

        addMouseListener(formListener);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.MouseListener {
        FormListener() {}
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            if (evt.getSource() == DrawPanel2.this) {
                DrawPanel2.this.formMouseClicked(evt);
            }
        }

        public void mouseEntered(java.awt.event.MouseEvent evt) {
        }

        public void mouseExited(java.awt.event.MouseEvent evt) {
        }

        public void mousePressed(java.awt.event.MouseEvent evt) {
        }

        public void mouseReleased(java.awt.event.MouseEvent evt) {
        }
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        clickPositionX = (evt.getPoint().x/zoom)/14;
        clickPositionY = (evt.getPoint().y/zoom)/14;
        this.repaint();
        parent.MouseClickedOnPanel(clickPositionX,clickPositionY);
    }//GEN-LAST:event_formMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    private void drawPuzzle(Graphics2D g2, PuzzleField data) {
        
        for (int i = 0; i < 300*14; i+=14) {
            for (int j = 0; j < 300*14; j+=14) {
                PuzzlePiece tmp = data.getPieceAt(j/14, i/14);
                if(tmp!=null){
                    g2.drawImage(tmp.getImage(),j-tmp.getOrigin().x-2,i-tmp.getOrigin().y-2, null);
                }             
            }
        }
    }
}
